package com.brave.mvvm.mvvmhelper.utils.generate

import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import androidx.annotation.IntRange
import androidx.annotation.Px

/**
 * ***author*** ：brave tou
 *
 * ***data***   : 2021/9/1 15:12
 *
 * ***desc***   : Drawable生成工具
 */
class DrawableGenerate {
    private var mView: View? = null

    // 背景颜色
    @ColorInt
    private var mBackgroundColor = Color.TRANSPARENT

    // 开始颜色
    @ColorInt
    private var mStartColor: Int? = null

    // 中间颜色
    @ColorInt
    private var mCenterColor: Int? = null

    // 结束颜色
    @ColorInt
    private var mEndColor: Int? = null

    // 渐变的形状类型
    private var mshape: Int = GradientDrawable.RECTANGLE

    // 渐变类型
    private var mGradientType: Int = GradientDrawable.LINEAR_GRADIENT

    // 渐变方向
    private var mGradientOrientation: GradientDrawable.Orientation =
        GradientDrawable.Orientation.LEFT_RIGHT

    // 边框宽度
    @Px
    private var mBorderWidth = 0

    // 边框颜色
    @ColorInt
    private var mBorderColor = Color.TRANSPARENT

    // 左上角圆角
    @Px
    private var mRoundedCornersLT = 0.0f

    // 右上角圆角
    @Px
    private var mRoundedCornersRT = 0.0f

    // 右下角圆角
    @Px
    private var mRoundedCornersRB = 0.0f

    // 左下角圆角
    @Px
    private var mRoundedCornersLB = 0.0f

    // 不透明度
    @IntRange(from = 0, to = 255)
    private var mAlpha: Int = 255

    private constructor()
    private constructor(mView: View?) {
        this.mView = mView
    }

    companion object {
        @JvmStatic
        @JvmOverloads
        fun with(mView: View? = null): DrawableGenerate {
            return if (null == mView) {
                DrawableGenerate()
            } else {
                DrawableGenerate(mView)
            }
        }
    }

    /**
     * 设置背景颜色
     */
    fun setBackgroundColor(@ColorInt color: Int): DrawableGenerate {
        this.mBackgroundColor = color
        return this
    }

    /**
     * 设置边框宽度
     */
    fun setBorderWidth(@Px width: Int): DrawableGenerate {
        this.mBorderWidth = width
        return this
    }

    /**
     * 设置边框颜色
     */
    fun setBorderColor(@ColorInt color: Int): DrawableGenerate {
        this.mBorderColor = color
        return this
    }

    /**
     * 设置边框
     */
    fun setBorder(@Px width: Int, @ColorInt color: Int): DrawableGenerate {
        this.mBorderWidth = width
        this.mBorderColor = color
        return this
    }

    /**
     * 设置圆角
     */
    fun setRoundedCorners(@Px roundedCorners: Float): DrawableGenerate {
        this.mRoundedCornersLT = roundedCorners
        this.mRoundedCornersRT = roundedCorners
        this.mRoundedCornersRB = roundedCorners
        this.mRoundedCornersLB = roundedCorners
        return this
    }

    /**
     * 设置圆角
     */
    fun setRoundedCorners(@Px roundedCorners: Int): DrawableGenerate {
        val corners = roundedCorners.toFloat()
        this.mRoundedCornersLT = corners
        this.mRoundedCornersRT = corners
        this.mRoundedCornersRB = corners
        this.mRoundedCornersLB = corners
        return this
    }

    /**
     * 设置圆角（左上角）
     */
    fun setRoundedCornersLT(@Px corners: Float): DrawableGenerate {
        this.mRoundedCornersLT = corners
        return this
    }

    /**
     * 设置圆角（左上角）
     */
    fun setRoundedCornersLT(@Px corners: Int): DrawableGenerate {
        this.mRoundedCornersLT = corners.toFloat()
        return this
    }

    /**
     * 设置圆角（右上角）
     */
    fun setRoundedCornersRT(@Px corners: Float): DrawableGenerate {
        this.mRoundedCornersRT = corners
        return this
    }

    /**
     * 设置圆角（右上角）
     */
    fun setRoundedCornersRT(@Px corners: Int): DrawableGenerate {
        this.mRoundedCornersRT = corners.toFloat()
        return this
    }

    /**
     * 设置圆角（右下角）
     */
    fun setRoundedCornersRB(@Px corners: Float): DrawableGenerate {
        this.mRoundedCornersRB = corners
        return this
    }

    /**
     * 设置圆角（右下角）
     */
    fun setRoundedCornersRB(@Px corners: Int): DrawableGenerate {
        this.mRoundedCornersRB = corners.toFloat()
        return this
    }

    /**
     * 设置圆角（左下角）
     */
    fun setRoundedCornersLB(@Px corners: Float): DrawableGenerate {
        this.mRoundedCornersLB = corners
        return this
    }

    /**
     * 设置圆角（左下角）
     */
    fun setRoundedCornersLB(@Px corners: Int): DrawableGenerate {
        this.mRoundedCornersLB = corners.toFloat()
        return this
    }

    /**
     * 设置透明度
     */
    fun setAlpha(@IntRange(from = 0, to = 255) alpha: Int): DrawableGenerate {
        this.mAlpha = alpha
        return this
    }

    /**
     * 设置不透明度
     */
    fun setAlpha(@FloatRange(from = 0.0, to = 1.0) alpha: Float): DrawableGenerate {
        this.mAlpha = (alpha * 255).toInt()
        return this
    }

    /**
     * 设置不透明度
     */
    fun setAlpha(@FloatRange(from = 0.0, to = 1.0) alpha: Double): DrawableGenerate {
        this.mAlpha = (alpha * 255).toInt()
        return this
    }

    /**
     * 设置渐变色
     */
    fun setGradientColor(@ColorInt vararg colors: Int): DrawableGenerate {
        val count = colors.size
        when {
            count >= 3 -> {
                this.mStartColor = colors[0]
                this.mCenterColor = colors[1]
                this.mEndColor = colors[2]
            }
            count >= 2 -> {
                this.mStartColor = colors[0]
                this.mEndColor = colors[1]
            }
            count >= 1 -> {
                this.mBackgroundColor = colors[0]
            }
            else -> {
            }
        }
        return this
    }

    /**
     * 设置渐变类型
     * @see GradientDrawable.LINEAR_GRADIENT 渐变是线性的
     * @see GradientDrawable.RADIAL_GRADIENT 渐变是循环的
     * @see GradientDrawable.SWEEP_GRADIENT 渐变是扫视的
     */
    @JvmOverloads
    fun setGradientType(gradientType: Int = GradientDrawable.LINEAR_GRADIENT): DrawableGenerate {
        this.mGradientType = gradientType
        return this
    }

    /**
     * 设置渐变方向
     * @see GradientDrawable.Orientation 枚举
     */
    @JvmOverloads
    fun setGradientOrientation(gradientOrientation: GradientDrawable.Orientation = GradientDrawable.Orientation.LEFT_RIGHT): DrawableGenerate {
        this.mGradientOrientation = gradientOrientation
        return this
    }

    /**
     * 設置渐变的形状类型
     * @see GradientDrawable.RECTANGLE 形状是一个矩形，可能有圆角
     * @see GradientDrawable.OVAL 形状是一个椭圆
     * @see GradientDrawable.LINE 形状是一条线
     * @see GradientDrawable.RING 形状是一个环
     */
    fun setShape(shape: Int = GradientDrawable.RECTANGLE): DrawableGenerate {
        this.mshape = shape
        return this
    }

    /**
     * 创建并赋值
     */
    fun create(): Drawable {
        val drawable = GradientDrawable()
        // 设置渐变的形状类型
        drawable.shape = mshape
        if (null == mStartColor && null == mEndColor) {
            // 设置背景颜色
            drawable.setColor(mBackgroundColor)
        } else {
            // 设置渐变色
            val colors = mutableListOf<Int>()
            if (null != mStartColor) colors.add(mStartColor!!)
            if (null != mCenterColor) colors.add(mCenterColor!!)
            if (null != mEndColor) colors.add(mEndColor!!)
            // 默认不偏移，均匀渐变
            drawable.colors = colors.toIntArray()
            // 设置渐变类型
            drawable.gradientType = mGradientType
            // 设置渐变角度
            drawable.orientation = mGradientOrientation
        }
        // 设置透明度
        drawable.alpha = mAlpha
        // 设置圆角
        drawable.cornerRadii = mutableListOf(
            mRoundedCornersLT, mRoundedCornersLT, // 左上角X、Y
            mRoundedCornersRT, mRoundedCornersRT, // 右上角X、Y
            mRoundedCornersRB, mRoundedCornersRB, // 右下角X、Y
            mRoundedCornersLB, mRoundedCornersLB, // 左下角X、Y
        ).toFloatArray()
        // 设置边框
        if (mBorderWidth > 0.0f) {
            drawable.setStroke(mBorderWidth, mBorderColor)
        }
        this.mView?.background = drawable
        return drawable
    }

    /**
     * 赋值
     */
    fun into(view: View) {
        this.mView = view
        create()
    }
}